package ${configs.packagePath}.aspect;

import ${configs.packagePath}.aspect.annotation.Paging;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;

@Component
@Aspect
@Slf4j
public class PagingAspect {

    /**
     * service包下分页切面
     *
     * @param joinPoint
     */
    @Before("execution(* ${configs.packagePath}.service.service..*.page*(Integer,Integer))")
    public void beforePage(JoinPoint joinPoint) throws NoSuchMethodException {
        paging(joinPoint);

    }

    @Before("@annotation(${configs.packagePath}.aspect.annotation.Paging)")
    public void beforePageAndSearch(JoinPoint joinPoint) throws NoSuchMethodException {
        paging(joinPoint);
    }

    private void paging(JoinPoint joinPoint) throws NoSuchMethodException {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Object target = joinPoint.getTarget();
        Method method = target.getClass()
                .getMethod(signature.getName(), signature.getParameterTypes());
        Paging paging = method.getDeclaredAnnotation(Paging.class);
        Object[] args = joinPoint.getArgs();
        log.debug("method params = {}", args);
        if (StringUtils.isEmpty(paging)) {
            PageHelper.startPage((Integer) args[0], (Integer) args[1]);
        }else{
            PageHelper.startPage((Integer) args[0], (Integer) args[1]);
        }
    }
//    /**
//     * 分页注解参数处理
//     *
//     * @param paging
//     * @param args
//     */
//    private void doAnnotationPaging(Paging paging, Object[] args) {
//        for (Object arg : args) {
//            BasePageDTO dto = convertParameter(arg);
//            if (dto != null) {
//                dto.setPage(paging.isPage());
//                if (dto.isPage()) {
//                    PageHelper.startPage(dto.getNowPage(), dto.getPageSize());
//                }
//            }
//        }
//    }
//
//    /**
//     * service包外注解分页
//     *
//     * @param joinPoint
//     * @throws NoSuchMethodException
//     */
//    @Before("@annotation(com.code.farming.db.aspect.annotation.Paging)")
//    public void paging(JoinPoint joinPoint) throws NoSuchMethodException {
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Object target = joinPoint.getTarget();
//        Method method = target.getClass()
//                .getMethod(signature.getName(), signature.getParameterTypes());
//        Paging paging = method.getDeclaredAnnotation(Paging.class);
//        Object[] args = joinPoint.getArgs();
//        log.debug("method params = {}", args);
//        doAnnotationPaging(paging, args);
//    }
//
//    /**
//     * 对参数进行转换和检查
//     *
//     * @param parameterObject 参数对象
//     * @return 分页对象
//     */
//    private BasePageDTO convertParameter(Object parameterObject) {
//        try {
//            if (parameterObject instanceof BasePageDTO) {
//                return (BasePageDTO) parameterObject;
//            } else {
//                if (parameterObject.getClass().isAssignableFrom(BasePageDTO.class)) {
//                    return (BasePageDTO) parameterObject;
//                }
//            }
//        } catch (Exception e) {
//            return null;
//        }
//        return null;
//    }
}
